/**
 * 
 */
package com.whf15.o2o.web.frontend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.whf15.o2o.dto.ProductExecution;
import com.whf15.o2o.dto.ShopExecution;
import com.whf15.o2o.entity.Product;
import com.whf15.o2o.entity.ProductCategory;
import com.whf15.o2o.entity.Shop;
import com.whf15.o2o.service.ProductCategoryService;
import com.whf15.o2o.service.ProductService;
import com.whf15.o2o.service.ShopService;
import com.whf15.o2o.util.HttpServletRequestUtil;

/**   
* 项目名称：o2o   
* 类名称：ShopDetailController   
* 类描述：   
* 创建人：whfo   
* 创建时间：2021年8月6日 下午3:34:37   
* @version        
*/
@Controller
@RequestMapping("/frontend")
public class ShopDetailController {
	@Autowired
	private ShopService shopService;
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductCategoryService productCategoryService;
	
	/**
	* 方法描述：   后取店铺信息一级该店铺下面的商品类别列表
	* 创建人：whfo   
	* 创建时间：2021年8月6日 下午3:46:44 
	* @param request
	* @return
	*/
	
	@RequestMapping(value="/listshopdetailpageinfo",method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> listShopDetailPageInfo(HttpServletRequest request){
		
		Map<String, Object> modelMap = new HashMap<String, Object>();
		//获取前台传过来的shopId
		long shopId = HttpServletRequestUtil.getLong(request, "shopId");
		Shop shop = null;
		List<ProductCategory> productCategoryList = null;
		if(shopId != -1) {
			//获取店铺Id为shopId的店铺信息
			shop = shopService.getByShopId(shopId);
			//获取店铺下面的商品类别列表
			productCategoryList = productCategoryService.getProductCategoryList(shopId);
			modelMap.put("shop", shop);
			modelMap.put("productCategoryList", productCategoryList);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty shopId");
		}
		return modelMap;
	}
	
	
	/**
	* 方法描述：   依据查询条件分页列出该店铺下面的所有商品
	* 创建人：whfo   
	* 创建时间：2021年8月6日 下午3:49:29 
	* @param request
	* @return
	*/
	@RequestMapping(value="/listproductsbyshop",method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> listProductsByShop(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<String, Object>();
		//获取页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取一页所需显示的条数
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		//获取店铺Id
		long shopId = HttpServletRequestUtil.getLong(request, "shopId");
		//空值判断
		if((pageIndex > -1) && (pageSize > -1) && (shopId > -1)) {
			//尝试获取商品类别Id
			long productCategoryId = HttpServletRequestUtil.getLong(request, "productCategoryId");	
			//尝试获取模糊查找的商品名
			String productName = HttpServletRequestUtil.getString(request, "productName");
			//组合查询条件
			Product productCondition = compactProductCondition4Search(shopId,productCategoryId,productName);
			//根据查询条件和分页信息获取商品列表，并返回总数
			ProductExecution pe  = productService.getProductList(productCondition, pageIndex, pageSize);
			modelMap.put("productList", pe.getProductList());
			modelMap.put("count",pe.getCount());
			modelMap.put("success",true);

		}else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty pageSize or pageIndex");
		}
		return modelMap;
		
	}


	/**
	* 方法描述：   组合查询条件，并将条件封装到productCondition对象返回
	* 创建人：whfo   
	* 创建时间：2021年8月6日 下午4:05:05 
	* @param shopId
	* @param productCategoryId
	* @param productName
	* @return
	*/
	private Product compactProductCondition4Search(long shopId, long productCategoryId, String productName) {
		Product productCondition = new Product();
		Shop shop = new Shop();
		shop.setShopId(shopId);
		productCondition.setShop(shop);
		
		if(productCategoryId != -1L) {
			//查询某个商品类别下面的商品列表
			ProductCategory productCategory = new ProductCategory();
			productCategory.setProductCategoryId(productCategoryId);
			productCondition.setProductCategory(productCategory);
		}
		if(productName != null) {
			//查询名字里包含productName的店铺列表
			productCondition.setProductName(productName);
		}
		//只允许选出状态为上架的商品
		productCondition.setEnableStatus(1);
		return productCondition;
		
	}
}
